package main

// 创建区块链
type BlockChain struct {
	blocks []*Block
}

func NewBlockChain() *BlockChain {
	// 创世区块
	genesisBlock := GenesisBlock()

	return &BlockChain{
		blocks: []*Block{genesisBlock},
	}

}

// 创世区快
func GenesisBlock() *Block {
	return NewBlock("创世区块", []byte{})
}

// 添加区块
func (bc *BlockChain) AddBlock(data string) {
	// 获取最后一个block 的hash
	lastBlock := bc.blocks[len(bc.blocks)-1]
	preHash := lastBlock.Hash
	// 创建区块并加入到区块链
	block := NewBlock(data, preHash)
	bc.blocks = append(bc.blocks, block)
}
